package com.shop.operator.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.beans.BeanUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.domain.CustomSeller;
import com.shop.domain.PageResult;
import com.shop.mapper.SellerMapper;
import com.shop.model.Seller;
import com.shop.model.SellerExample;
import com.shop.model.SellerExample.Criteria;
import com.shop.operator.service.OperatorSellerService;


@Service("operatorSellerService")
public class OperatorSellerServiceBean implements OperatorSellerService {

	@Resource
	private SellerMapper sellerMapper;
	
	

	@Override
	public PageResult findPage(Seller seller, Integer pageNum, Integer pageSize) throws Exception {
		PageHelper.startPage(pageNum, pageSize);//先把分页引进来
		SellerExample sellerExample = new SellerExample();
		//进行商家的条件拼接
		Criteria  criteria  = sellerExample.createCriteria();
		if(seller!=null){//如果传入的商家不为空
			//根据公司名进行查找
			if(seller.getName()!=null && seller.getName().length()>0){
				
				criteria.andNameLike("%"+seller.getName()+"%");
			}
			//根据商家店铺进行查找
			if(seller.getNickName()!=null && seller.getNickName().length()>0){
				
				criteria.andNickNameLike("%"+seller.getNickName()+"%");
				
			}
			//根据状态值进行查找
			if(seller.getStatus()!=null && seller.getStatus().length()>0){
				criteria.andStatusEqualTo(seller.getStatus());
			}
			
		}
		
		/*List<Seller> list = sellerMapper.selectByExample(sellerExample);
		System.out.println("返回集合="+list);*/
		//拿到page的分页对象
	        Page<Seller>  page  =(Page<Seller>) sellerMapper.selectByExample(sellerExample);
		
	      System.out.println("结果集："+page.getResult()+"总记录数："+page.getTotal());
	        
	      //返回分页结果
		 return new PageResult(page.getTotal(),page.getResult());
	}


	@Override
	public CustomSeller findAll(String id) throws Exception {
		
		Seller seller = this.sellerMapper.selectByPrimaryKey(id);
		CustomSeller customSeller =new CustomSeller();
		
		BeanUtils.copyProperties(seller, customSeller);
		
		return customSeller;
	}


	@Override
	public void updateStatus(String sellerId, String status) throws Exception {
		// TODO Auto-generated method stub
		Seller seller = this.sellerMapper.selectByPrimaryKey(sellerId);
		seller.setStatus(status);
		this.sellerMapper.updateByPrimaryKeySelective(seller);
	}

}
